﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Fluent;
using AvalonDock;
using Ookii.Dialogs.Wpf;

using SWCInvoice.Servicelayer;
using SWCInvoice.ViewModel;
using SWCInvoice.Model;
using System.Globalization;



namespace SWCInvoice
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        /// <summary>
        /// The main view model
        /// </summary>
        static MainViewModel _mainViewModel;

        #region Properties

        /// <summary>
        /// MainViewModel of the application
        /// </summary>
        public MainViewModel MainViewModel {
            get {
                return _mainViewModel;
            }
            set {
                _mainViewModel = value;
            }
        }
        
        #endregion

        public MainWindow()
        {
            CultureInfo cultureInfo = SWCInvoiceSettings.GetCultureInfo();
            
            Properties.ResourceHelp.Culture = cultureInfo;
            Properties.ResourceInputDialog.Culture = cultureInfo;
            Properties.ResourceLog.Culture = cultureInfo;
            Properties.ResourceMain.Culture = cultureInfo;
            Properties.ResourceMessages.Culture = cultureInfo;
            Properties.ResourcePdfView.Culture = cultureInfo;
            Properties.ResourceTextMessages.Culture = cultureInfo;
            Properties.ResourceVirtualDocumentStructureView.Culture = cultureInfo;
            System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
    
            InitializeComponent();

            _mainViewModel = new MainViewModel();
            _mainViewModel.MainWindow = this;

            DataContext = _mainViewModel;
          
            /*
            VirtualDocumentStructure vds;
            vds = PDFHandler.ExtractPDF(@"D:\Projekte\SWC-Invoice\Source\SWCInvoice\bin\Debug\test.pdf");
            //vds = PDFHandler.ExtractPDF(@"D:\Projekte\SWC-Invoice\Source\SWCInvoice\bin\Debug\Seiten aus Fehlerhafte Datei.pdf");            
            vds.ResultFolderPath = @"D:\Projekte\SWC-Invoice\Source\SWCInvoice\bin\Debug\";
            vds.ResultFolderPath = "Result";

            
            PDFHandler.SetPageIdentifier(vds.Documents.FindAll(p => p.IsOriginal), "Kundennummer", "[0-9]{8}");
            PDFHandler.SplitDucumentByIdentifier(vds.Documents[0]);
            PDFHandler.AddEmptyPage(vds.Documents, true, true);
            PDFHandler.CreatePDF(vds,false);
            */
        }

        #region Events

        #endregion

        #region Load/Unload Initial Layout

        private void dockingManager_Loaded(object sender, RoutedEventArgs e) {
            //if (File.Exists("layout.xml"))
            //    dockingManager.RestoreLayout(@"layout.xml");
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
            dockingManager.SaveLayout(@"layout.xml");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            ScreenTip.HelpPressed += OnScreenTipHelpPressed;
        }

        #endregion

        #region Dock Methods

        private void dockingManager_ActiveContentChanged(object sender, EventArgs e) {
        }

        private void logView_Loaded(object sender, RoutedEventArgs e) {
            //AddLogEntry("Welcome");
        }

        private void pdfView_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void messagesView_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
        
        #endregion

        #region Methods

        /// <summary>
        /// Set the busy indicator to true
        /// </summary>
        /// <param name="title">The title of the progress screen</param>
        public void SetIsBusy(string title = "Process ..."){
            this.busyIndicator.BusyContent = title;
            this.busyIndicator.IsBusy = true;
        }

        /// <summary>
        /// Set the busy indicator to false
        /// </summary>
        public void ResetIsBusy() {
            this.busyIndicator.IsBusy = false;
        }

        /// <summary>
        /// Handles F1 pressed on ScreenTip with help capability
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Arguments</param>
        static void OnScreenTipHelpPressed(object sender, ScreenTipHelpEventArgs e) {
            _mainViewModel.HelpCommand.Execute(e.HelpTopic.ToString());
        }

        #endregion              
   
    }
}
